package com.ea.service.impl;

import com.ea.common.constant.BaseNumConstant;
import com.ea.common.enums.BusinessTypeEnums;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.enums.SysCfgKeyEnums;
import com.ea.common.enums.YesOrNoEnum;
import com.ea.common.exception.ServiceException;
import com.ea.domain.dto.InventoryDTO;
import com.ea.domain.entity.ElectricGoodsSkuInfo;
import com.ea.domain.entity.SysBaseCfg;
import com.ea.domain.query.InventoryQueryDTO;
import com.ea.domain.vo.InventoryVO;
import com.ea.domain.entity.InventoryInfo;
import com.ea.mapper.ElectricGoodsSkuInfoMapper;
import com.ea.mapper.InventoryInfoMapper;
import com.ea.mapper.SysBaseCfgMapper;
import com.ea.service.IInventoryInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className IInventoryInfoServiceImpl
 * @date 2022-12-19 00:03:17
 */
@Service
public class IInventoryInfoServiceImpl implements IInventoryInfoService {
    private static final Logger logger = LoggerFactory.getLogger(IInventoryInfoServiceImpl.class);

    @Autowired
    private InventoryInfoMapper inventoryInfoMapper;

    @Autowired
    private SysBaseCfgMapper cfgMapper;

    @Autowired
    private ElectricGoodsSkuInfoMapper skuInfoMapper;

    @Override
    public PageInfo<InventoryVO> getInventoryList(InventoryQueryDTO queryDTO) {
        Integer pageIndex = queryDTO.getPageIndex() == null ? BaseNumConstant.NUM_1 : queryDTO.getPageIndex();
        Integer pageSize = queryDTO.getPageSize() == null ? BaseNumConstant.NUM_20 : queryDTO.getPageSize();
        PageHelper.startPage(pageIndex, pageSize);
        List<InventoryVO> list = inventoryInfoMapper.selectAllByCondition(queryDTO);
        return new PageInfo<>(list);
    }

    @Override
    public void updateInventoryInfoBatch(List<InventoryInfo> inventoryInfoList , String type) {
        if (CollectionUtils.isEmpty(inventoryInfoList)) {
            return;
        }
        logger.info("修改货品库存，修改类型为：{}", BusinessTypeEnums.getBusinessType(type));
        if (BusinessTypeEnums.STOCK_IN.getCode().equals(type)) {
            inventoryInfoMapper.updateAddInventoryBatchList(inventoryInfoList);
        }else if (BusinessTypeEnums.STOCK_OUT.getCode().equals(type)){
            inventoryInfoMapper.updateReduceInventoryBatchList(inventoryInfoList);
        }
    }

    @Override
    public void changeInventoryInfo(Long skuId, Integer amount) {
        List<InventoryInfo> inventoryInfoList = inventoryInfoMapper.selectAllBySkuIdList(Collections.singletonList(skuId));
        if (CollectionUtils.isEmpty(inventoryInfoList)) {
            ElectricGoodsSkuInfo skuInfo = skuInfoMapper.selectOneBySkuId(skuId);
            if (skuInfo == null) {
                throw new ServiceException(OperationResultCode.GOODS_SKU_NO_NOT_EXISTS.getCode(), String.format(OperationResultCode.GOODS_SKU_NO_NOT_EXISTS.getMsg(), skuId));
            }
            InventoryInfo info = new InventoryInfo();
            info.setSkuId(skuId);
            info.setSkuAmount(BigDecimal.valueOf(amount));
            info.setSkuName(skuInfo.getSkuName());
            info.setSkuBarcode(skuInfo.getSkuBarcode());
            inventoryInfoMapper.insertInventoryBatchList(Collections.singletonList(info));
        }
        InventoryInfo inventoryInfo = inventoryInfoList.get(0);
        BigDecimal newAmount = inventoryInfo.getSkuAmount().add(BigDecimal.valueOf(amount));
        int i = newAmount.compareTo(BigDecimal.ZERO);
        SysBaseCfg sysBaseCfg = cfgMapper.getSysBaseCfg(SysCfgKeyEnums.ALLOW_NEGATIVE_INVENTORY.getCfgKey());
        if (sysBaseCfg == null) {
            throw new ServiceException(OperationResultCode.SYS_CONFIG_ERROR.getCode(), String.format(OperationResultCode.SYS_CONFIG_ERROR.getMsg(), "是否允许负库存出库"));
        }
        if (i < 0) {
            if (YesOrNoEnum.NO.getCode().toString().equals(sysBaseCfg.getCfgValue())) {
                throw new ServiceException(OperationResultCode.SYS_CONFIG_STOCK_NEGATIVE);
            }
        }
        inventoryInfoMapper.updateInventoryInfoBySkuId(skuId, newAmount);
        logger.info("修改货品库存 ====> skuId:{} , amount:{}", skuId, newAmount);

    }

    @Override
    public List<InventoryDTO> getElectricSurplus() {
        return inventoryInfoMapper.queryFlowerStock();
    }
}
